package com.web.servlet.bean;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class DataObject {
	private final static Logger logger = LoggerFactory
			.getLogger(DataObject.class);
	
	//内部数据
	//表示是否为脏数据
	private boolean isDirty = false;
	public boolean getDirty() {
		return this.isDirty;
	}
	public void setDirty(boolean dirty) {
		this.isDirty = dirty;
	}
	
	public abstract boolean load();
	public abstract void save(); 
	private short op = 0;

	public final void setOp(short option) {
		if ((this.op == Option.Insert) && (option == Option.Update)) {
			return;
		}
		this.op = option;
	}

	public void resetUpdate() {
		this.op = Option.Update;
		setDirty(true);
	}

	public final short getOp() {
		return this.op;
	}

	public boolean beginAdd() {
		if (getOp() == Option.Insert) {
			setOp(Option.None);
			return true;
		}
		return false;
	}

	public void commitAdd(boolean result) {
		if (result == false) {
			setOp(Option.Insert);
			logger.error("添加出错了，状态还原" + toString());
		}
	}

	public boolean beginUpdate() {

		if (getOp() == Option.Update) {
			setOp(Option.None);
			return true;
		}
		return false;
	}

	public void commitUpdate(boolean result) {
		if (result == false) {
			setOp(Option.Update);
			logger.error("更新出错了，状态还原>>>" + toString());
		}
	}

}